'use strict';

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var productExceptSelf = function (nums) {
  return nums.map((num, index) => {
    return nums
      .filter((_, i) => i !== index)
      .reduce((acc, item) => acc * item, 1);
  });
};

// Example usage:
console.log(productExceptSelf([1, 2, 3, 4]));

// Time complexity: O(n)
// Space complexity: O(1)
